package com.ruoyi.app.service.imp;

import com.ruoyi.app.service.IDistributorService;
import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.domain.JtDistributor;
import com.ruoyi.business.service.IJtAppUserService;
import com.ruoyi.business.service.IJtDistributorService;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Alan Zhang
 * @Date 2025/4/2 11:35
 */
@Service
public class DistributorServiceImp implements IDistributorService {
    @Autowired
    private IJtDistributorService jtDistributorService;
    @Autowired
    private IJtAppUserService jtAppUserService;

    /**
     * 供应商申请
     * @param jtDistributor
     * @return
     */
    @Transactional
    @Override
    public AjaxResult applyDistributor(JtDistributor jtDistributor) {
        if(jtDistributor.getUserId() == null){
            return AjaxResult.error("用户ID不能为空");
        }
        JtDistributor param = new JtDistributor();
        param.setUserId(jtDistributor.getUserId());
        List<JtDistributor> jtDistributorList = jtDistributorService.selectJtDistributorList(param);
        if(jtDistributorList.size() > 0){
            return AjaxResult.error("已经申请过了！");
        }
        int rows = jtDistributorService.insertJtDistributor(jtDistributor);
        if (rows > 0) {
            //保存 用户申请
            JtAppUser jtAppUser = new JtAppUser();
            jtAppUser.setUserId(jtDistributor.getUserId());
            jtAppUser.setApplyStatus(1);
            int appUserRows = jtAppUserService.updateJtAppUser(jtAppUser);
            if(appUserRows > 0){
                return AjaxResult.success("申请成功");
            }
        }
        return AjaxResult.error("申请失败");
    }
}
